
const { check, validationResult } = require('express-validator'); //数据验证
const router = require('express').Router({

});
const adminControllers = require("../../controllers/adminControllers");
/* GET */
router.get(
    '/',
    async function (req, res, next) {
        let data = await adminControllers
                            .selectList(
                                req.query.pageNo,
                                req.query.pageSize,
                                req.query.courseName,
                                req.query.major,
                                req.query.className
                            );
                            
        res.sendResult(data, 200, "请求成功");

    }
);

router.post(
    '/',
    [
        check('admin_username').notEmpty().withMessage('admin_username参数不能为空'),
        check('admin_role_id').notEmpty().withMessage('admin_role_id参数不能为空'),
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }

        console.log(req.body);
        let data = await adminControllers.selectByName(req.body.admin_username);
        
        if(data){
            res.sendResult(null, 400, "添加失败,admin_username已存在");
            return;
        }
       
        await adminControllers.insert(req.body.admin_username,req.body.admin_role_id);
        res.sendResult({}, 201, "添加成功");
    }
);

router.put(
    '/',
    [
        check('admin_id').notEmpty().withMessage('admin_id参数不能为空'),
        check('admin_username').notEmpty().withMessage('admin_username参数不能为空'),
        check('admin_role_id').notEmpty().withMessage('admin_role_id参数不能为空'),
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }

        let data = await adminControllers.selectById(req.body.admin_id);
        if(!data){
            res.sendResult(null, 400, "更新失败,admin_id不存在");
            return;
        }
        
        if( ["root","admin"].includes(data.admin_username) ){
            res.sendResult(null, 400, "系统内置管理员,不可改变");
            return;
        }
        
        let data1 = await adminControllers.selectByName(req.body.admin_username,req.body.admin_id);
        
        if(data1){
            res.sendResult(null, 400, "更新失败,admin_username已存在");
            return;
        }

        await adminControllers.updateById(req.body.admin_id,req.body.admin_username,req.body.admin_role_id);
        res.sendResult({}, 202, "更新成功");
    }
);

router.delete(
    '/',
    [
        check('admin_id').notEmpty().withMessage('admin_id参数不能为空'),
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }
        console.log(req.query);
        

        let data = await adminControllers.selectById(req.query.admin_id);
        if(!data){
            res.sendResult(null, 400, "删除失败,admin_id不存在");
            return;
        }
        console.log(data);
        
        if( ["root","admin"].includes(data.admin_username) ){
            res.sendResult(null, 400, "系统内置管理员,不可删除");
            return;
        }
    
        await adminControllers.deleteByDid(req.query.admin_id);
        res.sendResult({}, 203, "删除成功");
    }
);
      
module.exports = router;
